<?php
session_start();
if(isset($_SESSION['userid'])&&$_SESSION['level']!=2)
{
	
	header ("location: login.php");
	exit();
}

?>
<?php
include("includes/top_user.php");
include("includes/top-panel.php");
?>
<div id="wrapper">
	<div id="content">
		<h3>Chỉnh sửa giao diện menu</h3>
		<?php include 'includes/connect.php';?>
    Nội dung trang :  <select id="menu_list" onchange="reloadContent(this);">
                                <?php
                                        $sql = "SELECT menu_id, menu_name FROM tb_menu WHERE menu_link <> 'product.php' ";
                                        $query = mysql_query($sql);
                                        $data_array = null;
                                        while ($data = mysql_fetch_array($query)):
                                            $data_array[] = $data[0];
                                ?>
                                        <option value="<?php echo $data["menu_id"]?>"><?php echo $data["menu_name"];?></option>
                                <?php endwhile;?>	
                      </select>
    <br>
    <br>
   <?php
      $sql_page = "SELECT p.page_content FROM tb_page_content p WHERE p.menu_id=".$data_array[0];
      $query_page = mysql_query($sql_page);

      while ($data_page = mysql_fetch_array($query_page)):
   ?>
    <form id="frEditor" method="POST" action="act/save_page_content.php">
      <textarea class="ckeditor" id="page_content" name="page_content" rows="70" cols="30" ><?php echo $data_page["page_content"]; ?></textarea>
    </form>
   <?php 
      endwhile;
   ?>         
   
   <br>
   <input style="width: 80px" type="button" onclick="saveContent();" value="Save" >
	</div>
	<?php
    include("includes/sidebar.php");
	?>
                   
</div>
<script type="text/javascript">
    function reloadContent(select_input){
      $.ajax({
        url : "act/reload_content.php",
        method : "POST",
        data : "menu_id="+$(select_input).val(),
        dataType : "json",
        success : function(obj){
          var a = "";
          if (obj.page_content!=null) {
              a=obj.page_content;
          }
            //console.log(obj.page_content);
            CKEDITOR.instances.page_content.setData(a);
          }
      });
    }
   
    function saveContent(){
        var menu_id = $("#menu_list").val();
        //Get value of text area ckeditor
        var data = CKEDITOR.instances.page_content.getData();
        var data_to_send = "menu_id="+$("#menu_list").val()+"&page_content="+encodeURIComponent(data);
        $.ajax({
          url : "act/save_page_content.php",
          method : "POST",
          data : data_to_send,
          dataType : "json",
          success : function(obj){
            if (obj==0) {
              alert("Cập nhật thành công...");
              location.reload();
            }
          }
           
      });
    }
</script>
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
<?php
include("includes/footer.php");
?>